package com.wyq.dao.impl;

import java.util.List;
import java.util.Map;

import com.wyq.bean.AdminUser;
import com.wyq.dao.AdminUserDAO;
import com.wyq.util.impl.JDBCDAOSupportImpl;

public class AdminUserDAOImpl extends JDBCDAOSupportImpl implements	AdminUserDAO {

	public List findAdminUserByPwd(AdminUser adminUser) throws Exception {
		String sql = "select * from admin_user where username = '" + adminUser.getUsername() 
						//+ "' and password = '" + adminUser.getPassword() + "'"
						+ "' and isavailable = 1";  
		List list = this.getListMapBySql(sql);
	/*	if(list.size() <= 0){
			return -1;
		}else{//用户名不可重复
			String password = list.get(0).toString();
			if(password.equals(adminUser.getPassword())){
				return 1;
			}else {
				return 0;
			}
		}*/
		return list;
	}
	
	public List<Map> getAdminUserInfoById(String id) throws Exception{
		String sql = "select * from admin_user where id = " + id;
		List<Map> list = this.getListMapBySql(sql);
		return list;
	}

	public int saveAdminUser(AdminUser adminUser) throws Exception {
		String sql = "insert into admin_user (username,password,isavailable) values('" 
						+ adminUser.getUsername() + "','"+adminUser.getPassword()+"',1)";
		int saveFlag = this.execupdateBySql(sql);
		return saveFlag;
	}

	public int updateAdminUser(Map adminUser) throws Exception {
		List<Map> list = this.getAdminUserInfoById("100");
		String sql = "";
		if(null == list || list.size()<=0){
			sql = "insert into admin_user (id,username,password,isavailable) values(" 
					+ "100"
					+ ",'"+adminUser.get("username")+"',"
					+ ",'"+adminUser.get("password")+"',"
					+ ",1)";
		}else{
			sql = "update admin_user set username = "
					+ "'"+adminUser.get("username")+"',"
					+ " and password = '" + adminUser.get("password")+"',"
					+ " and isavailable = " + adminUser.get("isavailable")
					+ " where id = " + list.get(0).get("id").toString();
		}
		
		
		int updateFlag = this.execupdateBySql(sql);
		return updateFlag;
	}

}
